<?php
// unneeded session_start();

class Display_family_model extends CI_Model{
    
    
    public function show_available_characters_for_play() {
        
        
        
        $query = $this->doctrine->em->createQuery("SELECT u 
                FROM ORM\Dynasties2\Characters u 
                WHERE u.isRuler = 1
                    AND u.deathDate IS NULL 
                    AND u.playerControlled = 0
                    ");
        return $query->getResult();
        
    }  //// end function show_available_characters_for_play
    
    public function show_all_living_family() {
        
        //$whichfamily = '12';
        $whichfamily = $_SESSION['active_family'];
        
        $query = $this->doctrine->em->createQuery("SELECT u 
                FROM ORM\Dynasties2\Characters u 
                WHERE u.whichFamily = $whichfamily 
                    AND (u.deathDate = 0 
                        OR u.deathDate IS NULL) ");
        //$result = $query->getResult();
         //print_r($result);
        return $query->getResult();
        
    }  //// end function show_all_living_family
    
    public function show_unmarried_family() {
        $whichfamily = $_SESSION['active_family'];
        
        // need to allow a player to only propose one betrothal per character at a time, so they don't spam it.
        // will likely require a join, since the Characters entity does not track whether they have a proposal out.
        // Heck, it doesn't even look to see if they are betrothed.
        
        $query = $this->doctrine->em->createQuery("SELECT u 
                FROM ORM\Dynasties2\Characters u 
                WHERE u.whichFamily = $whichfamily 
                    AND (u.deathDate = 0 
                        OR u.deathDate IS NULL) 
                    AND u.marriedTo = '0'
                ");
        return $query->getResult();
    } ////// end function show_unmarried_family
    
    public function show_potential_spouses() {
        $whichfamily = $_SESSION['active_family'];
        $char1 = $_SESSION["wedding_char1"];
        
        /// should probably have this not show characters who are already betrothed.  need a join?
        
        $character = $this->doctrine->em->find('ORM\Dynasties2\Characters', $char1);
        //echo $user->getfirst_Name().' '.$user->getlastname().' is a real chill guy.<br />';
        $is_female = $character -> getIsFemale();
        
        $query = $this->doctrine->em->createQuery("SELECT u 
                FROM ORM\Dynasties2\Characters u 
                WHERE u.whichFamily != $whichfamily 
                    AND (u.deathDate = 0 
                        OR u.deathDate IS NULL) 
                    AND u.marriedTo = '0'
                    AND u.isFemale != $is_female
                ");
        return $query->getResult();
        
        
    }   /// end function show_potential_spouses
    
    public function send_union_proposal() {
        
        $current_turn = '100';
        
        $char1 = $_SESSION["wedding_char1"];
        $char2 = $_SESSION["wedding_char2"];
        //echo $char1 . ", " . $char2 . "</br>";
        
        $new_proposal = new ORM\Dynasties2\Proposals;
        
        $new_proposal->setFromFamily($_SESSION['active_family']);
        $new_proposal->setToFamily($_SESSION["wedding_fam2"]);
        $new_proposal->setOnTurn($current_turn);
        $new_proposal->setTimeStamp(new DateTime());
        $new_proposal->setCharacter1($char1);
        $new_proposal->setCharacter2($char2);
        $new_proposal->setTargetTurn($_SESSION["target_turn"]);
        $new_proposal->setMoney('0');
        $new_proposal->setWedding('1');
        $new_proposal->setStatus('1');
        
        $this->doctrine->em->persist($new_proposal);
        $this->doctrine->em->flush();
        
    }  //// end function send_union_proposal
    
    public function load_proposals_from_me() {
        
        $whichfamily = $_SESSION['active_family'];
        $current_turn = '1';
        
        //$character = $this->doctrine->em->find('ORM\Dynasties2\Characters', $char1);
        //echo $user->getfirst_Name().' '.$user->getlastname().' is a real chill guy.<br />';
        //$is_female = $character -> getIsFemale();
        
        /*  //original,  working!
        $query = $this->doctrine->em->createQuery("SELECT u 
                FROM ORM\Dynasties2\Proposals u 
                WHERE u.fromFamily = $whichfamily 
                    AND u.targetTurn > $current_turn
                    AND u.status = '1'
                ");
         * 
         */
        
        // how about a join so that we get the charname as well
        // this...
        $query = $this -> doctrine -> em -> createQuery ("SELECT a, b, c
            FROM ORM\Dynasties2\Proposals a 
            JOIN a.character1 b
            JOIN a.character2 c
            WHERE a.fromFamily = $whichfamily 
                AND a.targetTurn > $current_turn
                AND a.status = '1'
            ");
        
        
        return $query->getResult();
        
    } /// end function load_proposals_from_me
    
    public function load_proposals_to_me() {
        
        
        
    } /// end function load_proposals_to_me
    
}